//
//  HeaderView.swift
//  clreader
//
//  Created by colin on 2019/3/27.
//  Copyright © 2019 colin. All rights reserved.
//

import Foundation
extension HomeViewController {
    class HeaderView: UIView {
        
        private var searchHandle:(()->Void)?
        private var scanHandle:(()->Void)?
        
        init(searchHandle:(()->Void)?,scanHandle:(()->Void)?) {
            super.init(frame: CGRect.zero)
            self.searchHandle = searchHandle
            self.scanHandle = scanHandle
            self.initViews()
        }
        
        func initViews() {
            self.backgroundColor = UIColor.white
            let container = UIView().then {
                $0.backgroundColor = UIColor(hex: "EFEFEF")
                
                $0.isUserInteractionEnabled = true
                let tap = UITapGestureRecognizer(target: self, action: #selector(self.actionSearch(_:)))
                $0.addGestureRecognizer(tap)
                
                // 图片
                let imageViewIcon = UIImageView(image: UIImage(named: "icon_search"))
                $0.addSubview(imageViewIcon)
                imageViewIcon.snp.makeConstraints({ (make) in
                    make.left.equalToSuperview().offset(15.0)
                    make.size.equalTo(CGSize(width: 16.0, height: 16.0))
                    make.centerY.equalToSuperview()
                })
                
                // 扫码
                let buttonScan = UIButton(type: .custom).then {
                    $0.setImage(UIImage(named: "icon_scan"), for: .normal)
                    $0.addTarget(self, action: #selector(self.actionScan(_:)), for: .touchUpInside)
                }
                $0.addSubview(buttonScan)
                buttonScan.snp.makeConstraints({ (make) in
                    make.top.bottom.equalToSuperview()
                    make.right.equalToSuperview()
                    make.width.equalTo(56.0)
                })
                
                // 分割线
                let line = UIView()
                line.backgroundColor = UIColor(hex: "DCDCDC")
                $0.addSubview(line)
                line.snp.makeConstraints({ (make) in
                    make.height.equalTo(20.0)
                    make.width.equalTo(0.5)
                    make.centerY.equalToSuperview()
                    make.right.equalTo(buttonScan.snp.left)
                })
                
                let labelTitle = UILabel().then {
                    $0.text = "知否?知否?"
                    $0.textColor = UIColor(hex: "828282")
                    $0.font = UIFont.systemFont(ofSize: 14.0, weight: .light)
                }
                $0.addSubview(labelTitle)
                labelTitle.snp.makeConstraints({ (make) in
                    make.centerY.equalToSuperview()
                    make.right.equalTo(line.snp.left).offset(-15.0)
                    make.left.equalTo(imageViewIcon.snp.right).offset(15.0)
                })
                
            }
            self.addSubview(container)
            container.snp.makeConstraints { (make) in
                make.height.equalTo(35.0)
                make.left.equalToSuperview().offset(20.0)
                make.right.equalToSuperview().offset(-20.0)
                make.top.equalToSuperview().offset(4.5)
                make.bottom.equalToSuperview().offset(-4.5)
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
}

extension HomeViewController.HeaderView {
    
    @objc private func actionSearch(_ sender:Any) {
        self.searchHandle?()
    }
    
    @objc private func actionScan(_ sender:Any) {
        self.scanHandle?()
    }
    
}
